#include<bits/stdc++.h>
using namespace std;
const int maxn = 50000+7, maxk = 100000+7;
int n, k;
int p[maxn], d[maxn];
int find(int x) {
  if(p[x] != x) {
    int t = find(p[x]);
    d[x] += d[p[x]];
    p[x] = t;
  }
  return p[x];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  cin>>n>>k;
  for(int i=0; i<=n; ++i) p[i] = i;
  int res = 0;
  while(k--) {
    int t, x, y; cin>>t>>x>>y;
    if(x>n || y>n) {
      res++;
      continue;
    }
    int px = find(x), py = find(y);
    if(t==1) {
      if(px==py && (d[x]-d[y])%3) {
        res++;
      }
      else if(px!=py) {
        p[px] = py;
        d[px]= d[y] - d[x];
      }
    }
    else {
      if(px==py && (d[x]-d[y]-1)%3) {
        res++;
      }
      else if(px!=py){
        p[px] = py;
        d[px] = d[y] - d[x] + 1;
      }
    }
  }
  cout<<res<<'\n';
  return 0;
}

